<template>
  <el-form :model="formData" ref="form" inline :label-width="labelWidth">
    <el-row :gutter="15">
      <el-col v-for="item in formItme" :key="item.prop" :span="item.span">
        <el-form-item
          class="item"
          :label="item.label"
          :prop="item.prop"
          :rules="item.rules"
        >
          <!-- 输入框 -->
          <el-input
            v-if="item.type === 'Input'"
            v-model="formData[item.prop]"
            :style="{ width: '100%' }"
          />
          <!-- 下拉框 -->
          <el-select
            v-if="item.type === 'Select'"
            v-model="formData[item.prop]"
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="op in item.options"
              :key="op.value"
              :label="op.label"
              :value="op.value"
            />
          </el-select>
          <el-date-picker
            v-if="item.type === 'data'"
            v-model="formData[item.prop]"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            type="date"
            :style="{ width: '100%' }"
            placeholder="选择日期"
          />
        </el-form-item>
      </el-col>
    </el-row>
    <!-- 按钮 -->
    <div class="btn">
      <el-form-item>
        <el-button
          v-for="item in formHandler"
          :key="item.label"
          :type="item.type"
          @click="item.handler && item.handler()"
          >{{ item.label }}</el-button
        >
      </el-form-item>
    </div>
  </el-form>
</template>

<script>
export default {
  name: "OrganForm",
  components: {},
  props: {
    labelWidth: {
      type: String,
      default: "auto"
    },
    formData: {
      type: Object,
      default: () => {}
    },
    formItme: {
      type: Array,
      default: () => []
    },
    // 按钮
    formHandler: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      type_msg: {
        Input: "请输入",
        data: "请选择",
        Select: "请选择"
      }
    }
  },
  methods: {
    // 重置表单
    resetForm() {
      this.$refs.form.resetFields()
    },
    initFormFData() {
      this.formItme.forEach(item => {
        // relus规则
        if (item.required) {
          this.relus(item)
        }
        // 自定义规则
        if (item.validator) {
          item.relus = item.validator
        }
      })
    },
    relus(item) {
      const requiredRelus = [
        {
          required: true,
          message: `${this.type_msg[item.type]}${item.label}`,
          trigger: "blur"
        }
      ]
      // 验证规则
      if (item.rulus && item.relus.length > 0) {
        item.relus = requiredRelus.concat(item.rules)
      } else {
        item.rules = requiredRelus
      }
    }
  },
  watch: {
    formItme: {
      handler(newValue) {
        this.initFormFData()
      },
      immediate: true
    }
  }
}
</script>
<style lang="scss" scoped>
.el-form {
  .el-row {
    display: flex;
    flex-wrap: wrap;
  }
  .item {
    width: 100%;
    display: flex;
    /deep/.el-form-item__content {
      flex: 1;
    }
  }
  .btn {
    text-align: right;
    .el-form-item {
      margin-top: 20px;
      margin-bottom: 0;
    }
  }
}
</style>
